from PySide6.QtWidgets import QDialog,QFileDialog,QMessageBox
from datetime import datetime
from sqlalchemy.orm import Session
from settings.publicvar import engine
from views.issuedmange.printmaterial.downdata.downdata import Ui_downdata
from openpyxl import load_workbook

class DownoadDataView(QDialog,Ui_downdata):
    def __init__(self,data,title):
        super().__init__()
        self.setupUi(self)
        self.data = data
        self.school = title[1]
        self.title = title[0]

        self.downloadbtn.clicked.connect(self.downexcelFun)

    def downexcelFun(self):
        data = self.data
        wb = load_workbook('state/model/XXX学校XXX工资XXXX年月.xlsx')
        ws = wb['Sheet1']
        ws.cell(1,1,self.school + self.title)
        ws.cell(2,1,'学校名称（公章）：'+self.school)
        datedata = datetime.now().strftime("%Y年%m月%d日")
        ws.cell(2,6,datedata)
        # if len(data) > 9:
        #     ws.insert_rows(idx=5,amount=len(data)-9)
        for i,item in enumerate(data):
            ws.cell(i+4,1,i+1)
            ws.cell(i+4,2,item['name'])
            ws.cell(i+4,3,item['IDcard'])
            ws.cell(i+4,4,item['cardNumber'])
            ws.cell(i + 4, 5, item['amount'])
            ws.cell(i + 4, 6, item['purpose'])
            ws.cell(i + 4, 7, item['paymenttype'])
        filepath,_ = QFileDialog.getSaveFileName(self,'保存',self.school + self.title +'.xlsx',"文本文件 (*.xlsx);;所有文件 (*)")
        if filepath:
            wb.save(filepath)
            self.close()
            QMessageBox.information(None,'成功','打印数据下载成功！')
        self.close()
